﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using QLNS_Public;
using QLNS_BLL;

namespace QLNS_GiaoDien
{
    public partial class uc_BaoHiem : DevExpress.XtraEditors.XtraUserControl
    {
        public uc_BaoHiem()
        {
            InitializeComponent();
        }
        BaoHiem_BLL baohiem_bll = new BaoHiem_BLL();
        BaoHiem_Public bh = new BaoHiem_Public();
        private void uc_BaoHiem_Load(object sender, EventArgs e)
        {
            gridControl1.DataSource = baohiem_bll.LayDuLieu();
        }
        int flag;
        private void btn_Them_Click(object sender, EventArgs e)
        {
            flag = 0;
        }

        private void btn_Sua_Click(object sender, EventArgs e)
        {
            flag = 1;
        }

        private void btn_Xoa_Click(object sender, EventArgs e)
        {
            bh.MaSoBaoHiem = Convert.ToInt32(gridView1.GetRowCellValue(gridView1.FocusedRowHandle,"Mã Số Bảo Hiểm"));
            if (XtraMessageBox.Show("Bạn có muốn xóa Mã số bảo hiểm: '" + bh.MaSoBaoHiem + "' này không?", "Thông Báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
            {
                baohiem_bll.delete_BaoHiem(bh);
                uc_BaoHiem_Load(sender,e);
            }
        }

        private void btn_Luu_Click(object sender, EventArgs e)
        {
            if (flag == 0)
            {
                bh.MaNhanVien = int.Parse(txt_MaNV.Text);
                bh.NgayCapSo = Convert.ToDateTime(dateTime_NgayCapSo.Text);
                bh.NoiCapSo = txt_NoiCapSo.Text;
                bh.GhiChu = txt_GhiChu.Text;
                baohiem_bll.Insert_BaoHiem(bh,bh,bh,bh);
                XtraMessageBox.Show("Thêm mới thành công!","Thông Báo",MessageBoxButtons.OK,MessageBoxIcon.Information);
            }
            if (flag == 1)
            {
                bh.MaSoBaoHiem = int.Parse(txt_MaSOBH.Text);
                bh.MaNhanVien = int.Parse(txt_MaNV.Text);
                bh.NgayCapSo = Convert.ToDateTime(dateTime_NgayCapSo.Text);
                bh.NoiCapSo = txt_NoiCapSo.Text;
                bh.GhiChu = txt_GhiChu.Text;
                baohiem_bll.Update_BaoHiem(bh,bh, bh, bh, bh);
                XtraMessageBox.Show("Sửa thành công!", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

        }

        private void txt_MaNV_TextChanged(object sender, EventArgs e)
        {
            string sql = "select tbl_NhanVien.HoTen,tbl_PhongBan.TenPhong from tbl_NhanVien,tbl_PhongBan where tbl_NhanVien.MaPhong=tbl_PhongBan.MaPhong and tbl_NhanVien.MaNhanVien='"+txt_MaNV.Text+"'";
            DataTable dt=new DataTable();
            dt=baohiem_bll.TaoBang(sql);
            cob_HoTen.DataSource = dt;
            cob_HoTen.DisplayMember = "HoTen";
            cob_PhongBan.DataSource = dt;
            cob_PhongBan.DisplayMember = "TenPhong";
        }
    }
}
